<template>
    <div class="calendar">
      <div class="header">
        <button @click="prevMonth">&lt;</button>
        <span @click="showMonthYearPicker"> {{ months[currentMonth] }} {{ currentYear }}</span>
        <button @click="nextMonth">&gt;</button>
      </div>
      <div class="picker" v-if="showPicker">
        <div>
          <select v-model="currentYear">
            <option v-for="year in years" :key="year" :value="year">{{ year }}</option>
          </select>
        </div>
        <div>
          <select v-model="currentMonth">
            <option v-for="month in months" :key="month" :value="month">{{ month }}</option>
          </select>
        </div>
        <button @click="hideMonthYearPicker">OK</button>
      </div>
      <div class="weekdays">
        <div v-for="day in daysOfWeek" :key="day">{{ day }}</div>
      </div>
      <div class="days">
        <div v-for="day in daysInMonth" :key="day" :class="{ 'not-current': day.month !== currentMonth }">
          {{ day.day }}
        </div>   
      </div>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        currentYear: new Date().getFullYear(),
        currentMonth: new Date().getMonth(),
        showPicker: false,
        daysOfWeek: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
        months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        years: Array.from({ length: 20 }, (_, i) => new Date().getFullYear() + i - 10),
      };
    },
    computed: {
      daysInMonth() {
        const days = [];
        const startDay = new Date(this.currentYear, this.currentMonth).getDay();
        const numDays = new Date(this.currentYear, this.currentMonth + 1, 0).getDate();
  
        for (let i = 0; i < startDay; i++) {
          days.push({ day: '', month: null });
        }
  
        for (let i = 1; i <= numDays; i++) {
          days.push({ day: i, month: this.currentMonth });
        }
  
        const remainingDays = 42 - days.length;
        for (let i = 0; i < remainingDays; i++) {
          days.push({ day: '', month: null });
        }
  
        return days;
      }
    },
    methods: {
  prevMonth() {
    this.currentMonth = (this.currentMonth === 0) ? 11 : this.currentMonth - 1;
    if (this.currentMonth === 11) this.currentYear--; // 如果从1月变为12月，则减去一年
  },
  nextMonth() {
    this.currentMonth = (this.currentMonth === 11) ? 0 : this.currentMonth + 1;
    if (this.currentMonth === 0) this.currentYear++; // 如果从12月变为1月，则加上一年
  },
  // 其他方法保持不变
      showMonthYearPicker() {
        this.showPicker = true;
      },
      hideMonthYearPicker() {
        this.showPicker = false;
      }
    }
  };
  </script>
  
  <style scoped>
  .calendar {
    width: 500px;
    max-width: 600px;
    margin: 0 auto;
    border: 1px solid #ccc;
  }
  
  .header {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    background-color: #f4f4f4;
  }
  
  .picker {
    position: absolute;
    top: 40px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #fff;
    border: 1px solid #ccc;
    padding: 10px;
    z-index: 10;
  }
  
  .weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    padding: 5px;
    background-color: #e4e4e4;
  }
  
  .days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    padding: 5px;
  }
  
  .days div {
    padding: 10px;
    text-align: center;
    border: 1px solid #ddd;
  }
  
  .not-current {
    opacity: 0.3;
  }
  </style>